package com.zzzy.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzzy.common.vo.AjaxResult;
import com.zzzy.common.vo.PageResult;
import com.zzzy.sys.dao.entity.Menu;
import com.zzzy.sys.dao.entity.Role;
import com.zzzy.sys.dao.entity.RoleDept;
import com.zzzy.sys.service.MenuService;
import com.zzzy.sys.service.RoleService;
import com.zzzy.sys.vo.menu.MenuTreeVO;
import com.zzzy.sys.vo.role.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "角色管理", description = "sys001")
@RestController
@Validated
@RequestMapping("/system/role")
public class RoleController {

    @Resource
    private RoleService roleService;
    @Resource
    private MenuService menuService;

    @Operation(summary = "角色分页列表")
    @GetMapping("/page")
    @PreAuthorize("@pvs.hasPermission('system:role:page')")
    public AjaxResult<PageResult<Role>> page(@Valid @ParameterObject RolePageReq queryReq) {
        PageResult<Role> rolePage = roleService.getRolePage(queryReq);
        return AjaxResult.success(rolePage);
    }

    @Operation(summary = "全部角色列表")
    @GetMapping("/list")
    @PreAuthorize("@pvs.hasPermission('system:role:page')")
    public AjaxResult<List<Role>> list() {
        List<Role> roleList = roleService.getBaseMapper().selectList(new LambdaQueryWrapper<Role>().orderByAsc(Role::getSort));
        return AjaxResult.success(roleList);
    }

    @PostMapping("/add")
    @Operation(summary = "新增角色")
    @PreAuthorize("@pvs.hasPermission('system:role:add')")
    public AjaxResult add(@Valid @RequestBody RoleAddReq addReq) {
        roleService.addRole(addReq);
        return AjaxResult.success();
    }

    @GetMapping(value = {"/getRole/{id}"})
    @Operation(summary = "获得角色信息")
    @Parameter(name = "id", description = "角色ID", required = true)
    @PreAuthorize("@pvs.hasPermission('system:role:page')")
    public AjaxResult<Role> getRole(@NotNull(message = "角色ID不能为空") @PathVariable("id") Long id) {
        Role role = roleService.getRoleById(id);
        return AjaxResult.success(role);
    }

    @PutMapping("/update")
    @Operation(summary = "修改角色")
    @PreAuthorize("@pvs.hasPermission('system:role:update')")
    public AjaxResult update(@Valid @RequestBody RoleUpdateReq updateReq) {
        roleService.updateById(updateReq);
        return AjaxResult.success();
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除角色")
    @Parameter(name = "id", description = "角色ID", required = true)
    @PreAuthorize("@pvs.hasPermission('system:role:delete')")
    public AjaxResult delete(@NotNull(message = "角色ID不能为空") @PathVariable("id") Long id) {
        roleService.deleteById(id);
        return  AjaxResult.success();
    }

    @GetMapping("/getRoleMenuIds/{id}")
    @Operation(summary = "获得角色菜单ID数组")
    @Parameter(name = "id", description = "角色ID", required = true)
    @PreAuthorize("@pvs.hasPermission('system:role:operateMenu')")
    public AjaxResult<List<Long>> getRoleMenuIds(@NotNull(message = "角色ID不能为空") @PathVariable("id") Long id) {
        List<Long> menuIdList = menuService.getMenuListByRoleId(id).stream().map(Menu::getId).collect(Collectors.toList());
        return AjaxResult.success(menuIdList);
    }

    @GetMapping("/getAllMenuTree")
    @Operation(summary = "获得所有菜单信息树形结构")
    @PreAuthorize("@pvs.hasPermission('system:role:operateMenu')")
    public AjaxResult<List<MenuTreeVO>> getAllMenuTree() {
        List<MenuTreeVO> menuList = menuService.getAllMenuTree();
        return AjaxResult.success(menuList);
    }

    @PostMapping("/operateMenu")
    @Operation(summary = "设置角色菜单权限")
    @PreAuthorize("@pvs.hasPermission('system:role:operateMenu')")
    public AjaxResult operateMenu(@Validated @RequestBody RoleMenuOperateReq operateReq) {
        roleService.operateMenu(operateReq);
        return AjaxResult.success();
    }

    @GetMapping("/getRoleDataDeptIds/{id}")
    @Operation(summary = "获得角色数据权限部门ID数组")
    @Parameter(name = "id", description = "角色ID", required = true)
    @PreAuthorize("@pvs.hasPermission('system:role:operateDataScope')")
    public AjaxResult<List<Long>> getRoleDataDeptIds(@NotNull(message = "角色ID不能为空") @PathVariable("id") Long id) {
        List<Long> menuIdList = roleService.getDataDeptIdListByRoleId(id).stream().map(RoleDept::getDeptId).collect(Collectors.toList());
        return AjaxResult.success(menuIdList);
    }

    @PostMapping("/operateDataScope")
    @Operation(summary = "设置角色数据权限")
    @PreAuthorize("@pvs.hasPermission('system:role:operateDataScope')")
    public AjaxResult operateDataScope(@Valid @RequestBody RoleDataScopeReq dataScopeReq) {
        roleService.operateDataScope(dataScopeReq);
        return AjaxResult.success();
    }
}
